Using historical closing prices of MSFT when was a good time to buy and sell based on the return on investment metric.
Return on investment:
(sold_price – purchase_price) / purchase_price
For example, if you buy 20 shares of Joe's Pizza for $10 a share, your investment cost is $200. 
If you sell those shares for $250, then your ROI is ($250-200)/$200 for a total of 0.25 or 25%. 
In [3]:
    
import pandas as pd
import datetime
%matplotlib inline
    
Q. Using all the historical closing prices of MSFT shares calculate which 60 day periods yielded a return on investment greater than 25%.
In [4]:
    
ls *.csv
    
    
In [5]:
    
stock = pd.read_csv('msft.csv', parse_dates=['Date']).set_index('Date').sort()
stock.tail()
    
    Out[5]:
In [6]:
    
stock["Open"].plot(figsize=(15, 5));
    
    
In [7]:
    
def ROI(series):
    """Return on investiment"""
    return (series[-1] - series[0]) / series[0]
    
In [8]:
    
days = 60 
stock["ROI"] = pd.rolling_apply(stock["Close"], days, ROI)
stock.tail()
    
    Out[8]:
In [9]:
    
good_times = stock[stock.ROI > 0.25]
good_times.tail()
    
    Out[9]:
In [10]:
    
ax = stock["Close"].plot(figsize=(15, 5));
ax.plot(good_times.index, good_times["Close"], 'or', alpha=0.3);
    
    
Q. Can you form a cost function to maximize ROI? Using scipy.optimize.
In [ ]: